﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.SessionState;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.MODEL.Custom_Model;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for TimeSheetManagementWithDetails
    /// </summary>
    public class TimeSheetManagementWithDetails : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string json = new StreamReader(context.Request.InputStream).ReadToEnd();

            var js = new JavaScriptSerializer();
            var work = js.Deserialize<OLTS.MODEL.OTS_TIME_SHEET>(json);
            work.EMPLOYEE_ID = BaseService.LOGIN.EMPLOYEE_ID;
            work.CREATED = DateTime.Now;
            work.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
            work.OTS_WORK_DETAIL = GetWorkDetail();
            var workservice = new TimeSheet_Service(work);
            workservice.Add();
        }

        private List<OLTS.MODEL.OTS_WORK_DETAIL> GetWorkDetail()
        {
            var work = new List<OLTS.MODEL.OTS_WORK_DETAIL>();
            var workSession = (List<OLTS.MODEL.OTS_WORK_DETAIL>)HttpContext.Current.Session["detail"];
            foreach (var d in workSession.ToList())
            {
                if (d.Action == ActionEnum.Create)
                {
                    var model = new OLTS.MODEL.OTS_WORK_DETAIL();

                    model.SEQ = d.SEQ;
                    model.AMOUNT_OF_TIME = d.AMOUNT_OF_TIME;
                    model.AMOUNT_OF_TIME_OVER_TIME = d.AMOUNT_OF_TIME_OVER_TIME;
                    model.DETAIL = d.DETAIL;
                    model.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
                    model.CREATED = DateTime.Now;
                    work.Add(model);
                }

            }
            return work;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}